package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.ApCommentDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author ItZheng
 * @Date 2023/6/17 19:43
 * @Version 1.5
 */
@Service
@Slf4j
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    /**
     * 修改AP端文章 上下架  0上架 1 下架
     *
     * @param map
     */
    @Override
    public void downOrUp(Map map) {

        Object enable = map.get("enable");

        boolean isDowm = true;
        if (enable.equals(1)) {
            isDowm = false;
        }

        //修改AP端文章属性：
        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId, map.get("articleId"))
                .set(ApArticleConfig::getIsDown, isDowm));

        log.info("修改参数成功：ID：{}，IsDowm：{}",map.get("articleId"),isDowm);

    }

    /**
     * 文章评论开启或关闭状态
     * @param dto
     * @return
     */
    @Override
    public ResponseResult isChannel(ApCommentDto dto) {
        //1.校验参数
        if (dto == null){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        boolean flag = false;
        //判断文章状态
        if (dto.getOperation() == 1){
            flag = true;
        }
        //处理数据,保存文章是否可以评论状态
        List<ApArticleConfig> apArticleConfigs = apArticleConfigMapper.selectList(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        for (ApArticleConfig apArticleConfig : apArticleConfigs) {
            apArticleConfig.setIsComment(flag);
            apArticleConfigMapper.updateById(apArticleConfig);
        }
        log.info("保存数据库");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 判断该文章是否开启评论
     * @param entryId
     * @return
     */
    @Override
    public ResponseResult stateComment(Long entryId) {
        //1.参数校验
        if (entryId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询数据库
        List<ApArticleConfig> apArticleConfigs = apArticleConfigMapper.selectList(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, entryId));

        if (apArticleConfigs == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticleConfig apArticleConfig = apArticleConfigs.get(0);
        //3.返回
        return ResponseResult.okResult(apArticleConfig);
    }
}
